﻿using LS.Screening.UI.Common;
using LS.Screening.UI.Common.Controls;
using LS.Screening.UI.Main.ViewModels;
using MaterialDesignThemes.Wpf;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace LS.Screening.UI.Main.Views
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : CommonWindow<MainViewModel>
    {
        public MainView()
        {
            InitializeComponent();

            var paletteHelper = new PaletteHelper();
            var theme = paletteHelper.GetTheme();

            CommonApplication app = (CommonApplication)Application.Current;
            switch (app.InitialTheme)
            {
                case BaseTheme.Dark:
                    ModifyTheme(true);
                    break;
                case BaseTheme.Light:
                    ModifyTheme(false);
                    break;
            }

            if (app.InitialFlowDirection == FlowDirection.RightToLeft)
            {
                // FlowDirectionToggleButton.IsChecked = true;
                FlowDirection = FlowDirection.RightToLeft;
            }

            DarkModeToggleButton.IsChecked = theme.GetBaseTheme() == BaseTheme.Dark;

            if (paletteHelper.GetThemeManager() is { } themeManager)
            {
                themeManager.ThemeChanged += (_, e) => DarkModeToggleButton.IsChecked = e.NewTheme?.GetBaseTheme() == BaseTheme.Dark;
            }
        }

        public MainView(string token, string serverUrl, string systemName, string authServerUrl, bool isNet = false)
            : this()
        {
            AppConfig.Instance.ServerUrl = serverUrl;
            AppConfig.Instance.SystemName = systemName;
            if (isNet == true)
            {
                AppConfig.Instance.RunLocation = RunLocation.Client;
                AppConfig.Instance.TokenName = "token";
                AppConfig.Instance.CrypKey = "Inaction";
                AppConfig.Instance.DataCompress = false;
            }
            else
            {

            }
            AppConfig.Instance.Save();
        }

        /// <summary>
        /// 菜单切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UIElement_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var dependencyObject = Mouse.Captured as DependencyObject;
            while (dependencyObject != null)
            {
                if (dependencyObject is ScrollBar) return;
                dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
            }
            MenuToggleButton.IsChecked = false;
        }

        /// <summary>
        /// 左侧菜单栏展开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuToggleButton_OnClick(object sender, RoutedEventArgs e)
        {
            DemoItemsSearchBox.Focus();
        }

        /// <summary>
        /// 皮肤切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuDarkModeButton_Click(object sender, RoutedEventArgs e)
        {
            ModifyTheme(DarkModeToggleButton.IsChecked == true);
        }

        /// <summary>
        /// 修改主题
        /// </summary>
        /// <param name="isDarkTheme"></param>
        private static void ModifyTheme(bool isDarkTheme)
        {
            var paletteHelper = new PaletteHelper();
            var theme = paletteHelper.GetTheme();

            theme.SetBaseTheme(isDarkTheme ? BaseTheme.Dark : BaseTheme.Light);
            paletteHelper.SetTheme(theme);
        }

        private void OnSelectedItemChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            MainScrollViewer.ScrollToHome();
        }
    }
}
